//
//  EFTipCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/2.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFTipCell.h"
#import "EFMessageTotalItem.h"
#import "BBCyclingLabel.h"
#import "EFTipItem.h"

@interface EFTipCell ()
@property (strong, nonatomic) UIImageView *backgroundImageV;
@property (strong, nonatomic) UIImageView *tipImageV;
@property (strong, nonatomic) UIButton *deletBtn;

@end

@implementation EFTipCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = RGBCOLOR(0xf5f9f9);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertViewShow)];
        [self addGestureRecognizer:tap];
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)initUI{
    [self.contentView addSubview:self.backgroundImageV];
    [self.backgroundImageV addSubview:self.tipImageV];
    [self.backgroundImageV addSubview:self.tipLabel];
    [self.backgroundImageV addSubview:self.deletBtn];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundImageV.top = 10;
    self.backgroundImageV.left = (kScreenWidth - self.backgroundImageV.width)/2;
    
    self.tipImageV.left = 8;
    self.tipImageV.top = (self.backgroundImageV.height - self.tipImageV.height)/2;
    
    self.deletBtn.top = (self.backgroundImageV.height - self.deletBtn.height)/2;
    self.deletBtn.right = self.backgroundImageV.width - 8;
    
}

- (void)setObject:(EFMessageTotalItem *)item index:(NSInteger)index{
    self.tipLabel.width = 280;
    self.tipLabel.height = 12;
    self.tipLabel.top = (self.backgroundImageV.height - self.tipLabel.height)/2;
    self.tipLabel.left = 28;
    EFTipItem *tipItem = (EFTipItem *)[item.list objectAtIndex:index];
    [self.tipLabel setText:tipItem.content animated:YES];
    self.currentMessage = tipItem.content;
}

- (UIImageView *)backgroundImageV{
    if (!_backgroundImageV) {
        _backgroundImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_notice_bg"]];
        _backgroundImageV.width = kScreenWidth - 2*20;
        _backgroundImageV.backgroundColor = [UIColor clearColor];
        _backgroundImageV.userInteractionEnabled = YES;
    }
    return _backgroundImageV;
}

- (UIImageView *)tipImageV{
    if (!_tipImageV) {
        _tipImageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"noticebar_icon_naotice_default"]];
        _tipImageV.userInteractionEnabled = true;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(alertViewShow)];
        [_tipImageV addGestureRecognizer:tap];
    }
    return _tipImageV;
}

- (UIButton *)deletBtn{
    if (!_deletBtn) {
        _deletBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
        [_deletBtn setImage:[UIImage imageNamed:@"noticebar_icon_close_default"] forState:UIControlStateNormal];
        [_deletBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deletBtn;
}

- (void)deleteBtnClick{
    if (self.deleteBlock != nil) {
        self.deleteBlock();
    }
}

- (void)alertViewShow{
    if (self.showBlock != nil) {
        self.showBlock(self.currentMessage);
    }
}

- (BBCyclingLabel *)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[BBCyclingLabel alloc] initWithFrame:CGRectMake(0, 0, 280, 12) andTransitionType:BBCyclingLabelTransitionEffectScrollUp];
        _tipLabel.textColor = RGBCOLOR(0x666666);
        _tipLabel.font = [UIFont systemFontOfSize:10];
        _tipLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        _tipLabel.textAlignment = NSTextAlignmentLeft;
        _tipLabel.transitionDuration = 0.3;
        _tipLabel.backgroundColor = [UIColor clearColor];
    }
    return _tipLabel;
}


@end
